package com.bwie.async;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class CreateAsync {
    @Bean("aaaaa")
    public Executor create(){
        System.out.println("线程池启动");
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(1); //线程数
        threadPoolTaskExecutor.setMaxPoolSize(1); //最大线程数
        threadPoolTaskExecutor.setQueueCapacity(0); //队列数
        threadPoolTaskExecutor.setKeepAliveSeconds(1000);//线程保持时间
        threadPoolTaskExecutor.setThreadNamePrefix("Async-"); //线程前缀名称
        //线程报错处理
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        threadPoolTaskExecutor.initialize();//线程初始化
        System.out.println("线程池初始化完成");
        return threadPoolTaskExecutor;
    }
}
